<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id="content">
    <button id="send">发送</button>
    <ul id="chatArea"></ul>
    <script>
        var sendBtn = document.getElementById('send');
        var input = document.getElementById('content');
        var chatArea = document.getElementById('chatArea');

        var ws = new WebSocket('ws://localhost:8080');

        ws.onopen = function () {
            console.log('创建socket成功');
        }

        ws.onmessage = function (e) {
            console.log(e.data);
            var li = document.createElement('li');
            li.innerHTML = e.data;
            chatArea.appendChild(li);
        }

        ws.onclose = function () {
            console.log('连接关闭');
        }


        sendBtn.onclick = function () {
            var content = input.value;
            ws.send(content);
        }
    </script>
</body>


</html>